{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Visualize data on quick-chart\n",
    "This notebook uses the [QuickChart](https://quickchart.io/) library to visualize data\n",
    "- configure the chart data\n",
    "- get chart as image\n",
    "- show and save the chart as jpg file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install quickchart.io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from quickchart import QuickChart\n",
    "from PIL import Image\n",
    "import requests\n",
    "from io import BytesIO\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import logging\n",
    "import sys\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Input parameters, chart type\n",
    "chart_type = os.environ.get('chart_type')\n",
    "\n",
    "# chart data    \n",
    "chart_data = os.environ.get('chart_data')\n",
    "\n",
    "# chart labels\n",
    "chart_labels = os.environ.get('chart_labels')\n",
    "\n",
    "# chart title\n",
    "chart_title = os.environ.get('chart_title')\n",
    "\n",
    "#chart height\n",
    "chart_height = os.environ.get('chart_height')\n",
    "\n",
    "#chart width\n",
    "chart_width = os.environ.get('chart_width')\n",
    "\n",
    "# path and file name for output\n",
    "output_data_image = os.environ.get('output_data_image')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = list(\n",
    "    map(lambda s: re.sub('$', '\"', s),\n",
    "        map(\n",
    "            lambda s: s.replace('=', '=\"'),\n",
    "            filter(\n",
    "                lambda s: s.find('=') > -1 and bool(re.match(r'[A-Za-z0-9_]*=[.\\/A-Za-z0-9]*', s)),\n",
    "                sys.argv\n",
    "            )\n",
    "    )))\n",
    "\n",
    "for parameter in parameters:\n",
    "    logging.warning('Parameter: ' + parameter)\n",
    "    exec(parameter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define your chart data configuration\n",
    "qc = QuickChart()\n",
    "qc.height = chart_height\n",
    "qc.width = chart_width\n",
    "qc.config = {\n",
    "    \"type\": chart_type,\n",
    "    \"data\": {\n",
    "        \"labels\": chart_labels,\n",
    "        \"datasets\": [{\n",
    "            \"label\": chart_title,\n",
    "            \"data\": chart_data\n",
    "        }]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print the chart url\n",
    "print(qc.get_short_url())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show the chart in this notebook, send a GET request to the image URL and retrieve the image\n",
    "response = requests.get(qc.get_short_url())\n",
    "image = Image.open(BytesIO(response.content))\n",
    "\n",
    "# Display the image\n",
    "plt.imshow(image)\n",
    "plt.axis('off')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#To save the image, convert the image to the 'RGB' mode if necessary\n",
    "if image.mode != 'RGB':\n",
    "    image = image.convert('RGB')\n",
    "    \n",
    "# Save the image to the specified file\n",
    "image.save(output_data_image)\n",
    "\n",
    "print(\"Image saved successfully!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
